package com.ssm.web.action;

import java.util.LinkedList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.bean.Course;
import com.ssm.util.Constant;
import com.ssm.util.FieldUtil;
import com.ssm.util.MessageUtil;
import com.ssm.util.Page;
import com.ssm.util.common.BaseAction;
import com.ssm.web.service.CourseService;
import com.ssm.web.service.TeacherService;

@Controller
@RequestMapping("/sys")
public class CourseAction extends BaseAction {
	@Autowired
	private TeacherService	teacherService;
	private String					actionname	= "课程";
	private String					actionclass	= "Course";
	@Autowired
	private CourseService		service;

	@RequestMapping(value = "/add2Course.do")
	public String add2() {
		putRequestValue("list", teacherService.selectAll());
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addCourse";
	}

	@RequestMapping(value = "/getCourse.do")
	public String get(int uid) {
		try {
			putRequestValue("list", teacherService.selectAll());
			Course temp = service.get(uid);
			request.setAttribute("modifybean", temp);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyCourse";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/addCourse.do")
	public String add(Course bean) {
		try {
			service.add(bean);
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateCourse.do")
	public String update(Course bean) {
		try {
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteCourse.do")
	public String delete(String ids) {
		try {
			service.deleteAll(ids);
			MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "操作失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryCourse.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Course.class, parmnames, parmvalues);

			Page page = service.selectPage(p, Course.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listCourse";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
